package com.szxx.suit.three;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.szxx.constant.DdzCardTypes;
import com.szxx.domain.Card;
import com.szxx.domain.Cards;
import com.szxx.domain.DdzCard;
import com.szxx.suit.AbstractSuitTerm;
import com.szxx.suit.AbstractSuitType;
import com.szxx.suit.DdzAnalysisResult;
import com.szxx.suit.DdzSuitAnalyzer;
import com.szxx.suit.DdzTypeUtil;

/**
 * 三张牌型 <br>
 *
 * @author Ambrose
 */
@Component
public class ThreeTypeAnalyzer extends DdzSuitAnalyzer {

	@Override
	public AbstractSuitType getSuitType() {
		return DdzCardTypes.THREE;
	}

	/** 关键数量 */
	public static int TUPLE_SIZE = 3;
	
	@Override
	protected boolean doCheckRule(Cards cards, AbstractSuitTerm term, DdzAnalysisResult result) {
		if (result.getCardNum() == TUPLE_SIZE) {
			if (result.getLaziCardNum() == 0 || result.isAllCardLazi()) { // 没有癞子，都是癞子
				return result.getAllValueCounter().getValuesInCount(3).size() == 1;
			} else {
				if (result.getLaziCardNum() == 1) { // 一张癞子，非赖中有一对
					return result.getNoneLaziValueCounter().getValuesInCount(2).size() == 1;
				} else { // 两张癞子，非赖中不能有王
					if (result.getNoneLaziValueCounter().hasJoker()) { // 非赖中含有大小王
						return false;
					} else {
						return true;
					}
				}
			}
		} else {
			return false;
		}
	}
	
	@Override
	protected List<Cards> doAnalysizeLaizi(Cards cards, AbstractSuitTerm term, boolean onlyMaxValue) {
		List<Cards> cardsResult = new LinkedList<Cards>();
		DdzAnalysisResult result = (DdzAnalysisResult) cards.getResult();
		List<Card> laiziCardList = new ArrayList<Card>(cards.subList(result.getCardNum() - result.getLaziCardNum(), result.getCardNum()));
		int value = result.getNoneLaziValueCounter().getAllValues().get(0).intValue();
		for (Card card : laiziCardList) { // 设置number
			((DdzCard) card).changeCard(value);
		}
		Cards cardsCopy = cards.clone();
		analysize(cardsCopy, term);
		cardsResult.add(cardsCopy);
		return cardsResult;
	}
	
	@Override
	protected boolean containsPossibleCards(Cards handCards, AbstractSuitTerm term) {
		DdzAnalysisResult result = (DdzAnalysisResult) handCards.getResult();
		return result.getCardNum() - result.getJokerCardNum() >= TUPLE_SIZE;
	}
	
	/** 最多使用癞子的数量 */
	private static int MAX_USE_LAIZI_CARD_NUM = 3;
	
	@Override
	protected List<Cards> doListPossibleCards(Cards handCards, AbstractSuitTerm term) {
		List<Cards> cardsResult = new LinkedList<Cards>();
		// 非癞子部分
		DdzAnalysisResult result = (DdzAnalysisResult) handCards.getResult();
		List<Card> noneLaiziCardList = handCards.subList(0, result.getNoneCardNum());
		Cards noneLaiziCards = DdzTypeUtil.createCardsCopy(noneLaiziCardList);
		noneLaiziCards.setResult(handCards.getResult().clone());
		// 癞子部分
		List<Card> laiziCardList = handCards.subList(result.getNoneCardNum(), result.getCardNum());
		Cards laiziCards = DdzTypeUtil.createCardsCopy(laiziCardList);
		laiziCards.setResult(handCards.getResult().clone());
		
		int laiziCardNum = result.getLaziCardNum();
		int maxUseLaiziCardNum = MAX_USE_LAIZI_CARD_NUM < laiziCardNum ? MAX_USE_LAIZI_CARD_NUM : laiziCardNum;
		for (int useLaiziCardNum = 0; useLaiziCardNum <= maxUseLaiziCardNum; useLaiziCardNum++) {
			int needCardNum = TUPLE_SIZE - useLaiziCardNum;
			if (needCardNum > 0) { // 部分用癞子的情况
				List<Card> useLaiziCardList = DdzTypeUtil.cloneSublistedCards(laiziCards, 0, useLaiziCardNum);// 补癞子
				List<Cards> cardsList = new LinkedList<Cards>();
				if (needCardNum == 1) {
					cardsList.addAll(DdzTypeUtil.selectSingleCards(noneLaiziCards, false));
				} else {
					cardsList.addAll(DdzTypeUtil.selectTupleCountCards(noneLaiziCards, needCardNum));
				}
				for (Cards usecards : cardsList) {
					Card card = usecards.get(0);
					for (Card laiziCard : useLaiziCardList) {
						((DdzCard)laiziCard).changeCard(card.getValue());
					}
					Cards cardsCopy = DdzTypeUtil.cloneCardsAndAddCardList(usecards, useLaiziCardList);
					if (match(cardsCopy, term)) {
						analysize(cardsCopy, term);
						cardsResult.add(cardsCopy);
					}
				}
			} else { // 全部用癞子的情况
				List<Cards> useLaiziCardList = DdzTypeUtil.selectTupleCountCards(laiziCards, TUPLE_SIZE);
				for (Cards useLaiziCards : useLaiziCardList) {
					for (Card card : useLaiziCards) {
						((DdzCard)card).changeCard(card.getNumber());
					}
					Cards cardsCopy = DdzTypeUtil.cloneCardsAndAddCardList(useLaiziCards, null);
					if (match(cardsCopy, term)) {
						analysize(cardsCopy, term);
						cardsResult.add(cardsCopy);
					}
				}
			}
		}
		return cardsResult;
	}
	
//	@Override
//	protected List<Cards> doListPossibleCards(Cards handCards, AbstractSuitTerm term, boolean onlyMaxValue) {
//		List<Cards> cardsResult = new LinkedList<Cards>();
//		DdzAnalysisResult result = (DdzAnalysisResult) handCards.getResult();
//		List<Card> noneLaiziCardList = handCards.subList(0, result.getNoneCardNum());
//		Cards noneLaiziCards = DdzTypeUtil.createCloneCards(noneLaiziCardList);
//		noneLaiziCards.setResult(handCards.getResult().clone());
//		// 非癞子部分寻找三张
//		cardsResult.addAll(DdzTypeUtil.selectTupleCountCards(noneLaiziCards, 3));
//		if (result.getLaziCardNum() >= 1) { // 考虑癞子
//			List<Card> laiziCardList = handCards.subList(result.getNoneCardNum(), result.getCardNum());
//			Cards laiziCards = DdzTypeUtil.createCloneCards(laiziCardList);
//			laiziCards.setResult(handCards.getResult().clone());
//			// 补一张癞子
//			List<Card> use_1_LaiziCardList = DdzTypeUtil.cloneSublistCards(laiziCards, 0, 1);
//			List<Cards> pairCardList = DdzTypeUtil.selectTupleCountCards(noneLaiziCards, 2);
//			for (Cards cards : pairCardList) {
//				Card card = cards.get(0);
//				for (Card laiziCard : use_1_LaiziCardList) {
//					((DdzCard)laiziCard).changeCard(card.getValue());
//				}
//				cardsResult.add(DdzTypeUtil.createCloneCards(cards, use_1_LaiziCardList));
//			}
//			if (result.getLaziCardNum() >= 2) {
//				// 补两张癞子
//				List<Card> use_2_LaiziCardList = DdzTypeUtil.cloneSublistCards(laiziCards, 0, 2);
//				List<Cards> singleCardList = DdzTypeUtil.selectSingleCards(noneLaiziCards, false);
//				for (Cards cards : singleCardList) {
//					Card card = cards.get(0);
//					for (Card laiziCard : use_2_LaiziCardList) {
//						((DdzCard)laiziCard).changeCard(card.getValue());
//					}
//					cardsResult.add(DdzTypeUtil.createCloneCards(cards, use_2_LaiziCardList));
//				}
//				if (result.getLaziCardNum() >= 3) {
//					// 癞子部分寻找三张
//					List<Cards> threeLaiziCardList = DdzTypeUtil.selectTupleCountCards(laiziCards, 3);
//					for (Cards cards : threeLaiziCardList) {
//						for (Card laiziCard : cards) {
//							((DdzCard)laiziCard).changeCard(laiziCard.getNumber());
//						}
//					}
//					cardsResult.addAll(threeLaiziCardList);
//				}
//			}
//		}
//		return cardsResult;
//	}

}
